package com.billdowney.service.impl.user;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.billdowney.dao.user.UserInfoDao;
import com.billdowney.dao.user.UserLoginInfoDao;
import com.billdowney.entity.base.EntityStatus;
import com.billdowney.entity.user.UserInfo;
import com.billdowney.entity.user.UserLoginInfo;
import com.billdowney.exception.BizException;
import com.billdowney.service.impl.base.BaseServiceImpl;
import com.billdowney.service.interfaces.user.UserInfoService;
import com.billdowney.service.interfaces.user.UserInfoValidateService;
import com.billdowney.util.CommonUtil;
import com.billdowney.util.regex.ValidationUtil;
import com.billdowney.util.return_type.DataPaging;
import com.billdowney.util.return_type.PagingUtil;

/**
 * @ClassName: UserInfoServiceImpl
 * @Description: 用户信息处理接口实现
 * @author 超级小富翁 - BillDowney
 * @date 2018年2月1日 下午6:31:55
 */
@Service
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfoDao, UserInfo, String> implements UserInfoService {

	@Autowired
	private UserLoginInfoDao userLoginInfoDao;
	@Autowired
	private UserInfoValidateService userInfoValidateService;
	// 加密工具
	@Autowired
	private PasswordEncoder passwordEncoder;

	@Resource(type = UserInfoDao.class)
	@Override
	public void setDao(UserInfoDao dao) {
		super.dao = dao;
	}

	@Override
	public UserInfo addUserLoginInfo(UserLoginInfo userLoginInfo) throws Exception {
		if (CommonUtil.isEmpty(userLoginInfo.getUserInfo())) {
			userLoginInfo.setUserInfo(new UserInfo());
		}
		// 判断密码是否为空
		if (CommonUtil.isEmpty(userLoginInfo.getPassword())) {
			throw new BizException(10201);
		}
		// 判断用户名、手机号或电子邮件是否为空，格式是否正确
		else if (CommonUtil.isEmpty(userLoginInfo.getUsername())
				&& !ValidationUtil.mobileTelephone(userLoginInfo.getPhone())
				&& !ValidationUtil.email(userLoginInfo.getEmail())) {
			throw new BizException(10202);
		}
		userLoginInfo.setOrigPassword(userLoginInfo.getPassword());
		userLoginInfo.setPassword(passwordEncoder.encode(userLoginInfo.getOrigPassword()));
		userInfoValidateService.validateUserLoginInfoRepeatField(userLoginInfo);
		try {
			userLoginInfoDao.save(userLoginInfo);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BizException(10223);
		}
		return userLoginInfo.getUserInfo();
	}

	@Override
	public DataPaging queryUserByPaging(int pageNum, int pageSize, String keyword, EntityStatus status) {
		if (CommonUtil.isEmpty(keyword)) {
			keyword = "";
		}
		return PagingUtil.getDataPaging(
				userLoginInfoDao.findAll(PagingUtil.getPageable(pageNum, pageSize), status, "%" + keyword + "%"));
	}

	@Override
	public int delUser(String... ids) throws Exception {
		if (CommonUtil.isEmpty(ids)) {
			throw new BizException(10231);
		}
		return userLoginInfoDao.updateStatus(EntityStatus.delete, new Date(), CommonUtil.arrayToList(ids));
	}

}
